package com.utils
{
	import flash.display.DisplayObject;
	import flash.filters.ColorMatrixFilter;

	public class ColorUtils
	{
		public function ColorUtils()
		{
		}
		
		/**
		 *  执行一个颜色的亮度的调节. 
		 *
		 *  <p>这个RGB颜色的每个值都必须在0-255之间.</p>
		 *
		 *  @param rgb 原始的RGB颜色.
		 *
		 *  @param brite 增加每个颜色的值.
		 *  这个参数的范围必须是-255至255之间.
		 *  当值为255时为白色，-255时为黑色.
		 *  如果这个值为0，则返回原值.
		 *  @return 新的 RGB 颜色.
		 */
		public static function adjustBrightness(rgb:uint, brite:Number):uint
		{
			var r:Number = Math.max(Math.min(((rgb >> 16) & 0xFF) + brite, 255), 0);
			var g:Number = Math.max(Math.min(((rgb >> 8) & 0xFF) + brite, 255), 0);
			var b:Number = Math.max(Math.min((rgb & 0xFF) + brite, 255), 0);
			
			return (r << 16) | (g << 8) | b;
		} 
		
		public static function blackFilter(target:DisplayObject,flag:Boolean):void{
			if(flag){
				target.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0,
					0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0])];
			}else{
				target.filters = null;
			}
		}
		
		
		private function RGB2HSL(r:Number, g:Number, b:Number):Array
		{
			var H:Number;
			var S:Number;
			var B:Number;
			
			var cmax:Number = (r > g) ? r : g;
			if (b > cmax) {
				cmax = b;
			}
			var cmin:Number = (r < g) ? r : g;
			if (b < cmin) {
				cmin = b;
			}
			B = cmax;
			if (cmax != 0) {
				S = (cmax - cmin)/cmax;
			} else {
				S = 0;
			}
			if (S == 0) {
				H = 0;
			} else {
				var redc:Number   = (cmax - r)/(cmax - cmin);
				var greenc:Number = (cmax - g)/(cmax - cmin);
				var bluec:Number  = (cmax - b)/(cmax - cmin);
				if (r == cmax) {
					H = bluec - greenc;
				} else if (g == cmax) {
					H = 2.0 + redc - bluec;
				} else {
					H = 4.0 + greenc - redc;
				}
				H = H / 6.0;
				if (H < 0) {
					H = H + 1.0;
				}
			}
			return [ H, S, B ];
		}
		
		public static function getStringColor(color:int):String{
			var str:String = color.toString(16);
			while(str.length<6){
				str = "0"+str;
			}
			return "#"+str;
		}
	}
}